package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BizPurchaseOrder;
import com.ruoyi.system.service.IBizPurchaseOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.BizPurchaseOrderItemView;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 采购订单Controller
 * 
 * @author ruoyi
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/system/purchase_order")
public class BizPurchaseOrderController extends BaseController
{
    @Autowired
    private IBizPurchaseOrderService bizPurchaseOrderService;

    @Autowired
    private com.ruoyi.web.support.SupplierContext supplierContext;

    /**
     * 查询采购订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPurchaseOrder bizPurchaseOrder)
    {
        // 若为供应商角色，强制按其供应商编码过滤
        supplierContext.resolveCurrentSupplierCode().ifPresent(bizPurchaseOrder::setSupplierCode);
        startPage();
        List<BizPurchaseOrder> list = bizPurchaseOrderService.selectBizPurchaseOrderList(bizPurchaseOrder);
        return getDataTable(list);
    }

    /**
     * 查询采购订单明细联表列表（用于前端明细视图）
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:list')")
    @GetMapping("/items")
    public TableDataInfo listItems(BizPurchaseOrderItemView view)
    {
        supplierContext.resolveCurrentSupplierCode().ifPresent(view::setSupplierCode);
        startPage();
        List<BizPurchaseOrderItemView> list = bizPurchaseOrderService.selectBizPurchaseOrderItemJoinedList(view);
        return getDataTable(list);
    }

    /**
     * 更新采购订单明细状态（如 QUALITY_PASSED / QUALITY_FAILED）
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:edit')")
    @Log(title = "采购订单明细状态", businessType = BusinessType.UPDATE)
    @PutMapping("/items/{itemId}/status/{itemStatus}")
    public AjaxResult updateItemStatus(@PathVariable("itemId") Long itemId,
                                       @PathVariable("itemStatus") String itemStatus) {
        return toAjax(bizPurchaseOrderService.updateBizPurchaseOrderItemStatus(itemId, itemStatus));
    }

    /**
     * 导出采购订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:export')")
    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPurchaseOrder bizPurchaseOrder)
    {
        supplierContext.resolveCurrentSupplierCode().ifPresent(bizPurchaseOrder::setSupplierCode);
        List<BizPurchaseOrder> list = bizPurchaseOrderService.selectBizPurchaseOrderList(bizPurchaseOrder);
        ExcelUtil<BizPurchaseOrder> util = new ExcelUtil<BizPurchaseOrder>(BizPurchaseOrder.class);
        util.exportExcel(response, list, "采购订单数据");
    }

    /**
     * 获取采购订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(bizPurchaseOrderService.selectBizPurchaseOrderByOrderId(orderId));
    }

    /**
     * 新增采购订单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:add')")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPurchaseOrder bizPurchaseOrder)
    {
        return toAjax(bizPurchaseOrderService.insertBizPurchaseOrder(bizPurchaseOrder));
    }

    /**
     * 修改采购订单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:edit')")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPurchaseOrder bizPurchaseOrder)
    {
        return toAjax(bizPurchaseOrderService.updateBizPurchaseOrder(bizPurchaseOrder));
    }

    /**
     * 删除采购订单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase_order:remove')")
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(bizPurchaseOrderService.deleteBizPurchaseOrderByOrderIds(orderIds));
    }
}
